package com.sgu6.sgu6_machine.db.converter;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.sgu6.sgu6_machine.dto.GoodsSimpleDto;

import org.greenrobot.greendao.converter.PropertyConverter;

import java.util.ArrayList;
import java.util.List;

public class PhotoBeanConverter implements PropertyConverter<List<GoodsSimpleDto.PhotoBean>, String> {
    @Override
    public List<GoodsSimpleDto.PhotoBean> convertToEntityProperty(String databaseValue) {
        if (databaseValue == null) {
            return null;
        }
        JsonArray jsonArray = new JsonParser().parse(databaseValue).getAsJsonArray();
        Gson gson = new Gson();
        ArrayList<GoodsSimpleDto.PhotoBean> photoBeanList = new ArrayList<>();
        //循环遍历
        for (JsonElement element : jsonArray) {
            //通过反射 得到UserBean.class
            GoodsSimpleDto.PhotoBean photoBean = gson.fromJson(element, GoodsSimpleDto.PhotoBean.class);
            photoBeanList.add(photoBean);
        }
        return photoBeanList;
    }

    @Override
    public String convertToDatabaseValue(List<GoodsSimpleDto.PhotoBean> entityProperty) {
        return new Gson().toJson(entityProperty);
    }
}